package com.yylc.cscore.integration;

import cn.hutool.core.util.StrUtil;
import com.yylc.cif.common.service.api.UserQueryService;
import com.yylc.cif.common.service.model.UserInfo;
import com.yylc.platform.common.base.QueryResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author xiangnan
 * date 2018/5/8
 */
@Component
public class UserQueryServiceClient {
    private static final Logger logger = LogManager.getLogger();

    @Resource
    private UserQueryService userQueryService;

    @Resource
    private ServiceClient serviceClient;

    public UserInfo queryByUserId(String userId) {
        if (StrUtil.isBlank(userId)) {
            return null;
        }

        return serviceClient.handle(() -> {
            QueryResult<UserInfo> result = userQueryService.queryByUserId(userId);
            if (result.isSuccess()) {
                return result.getResultObject() == null ? mockUserInfo(userId) : result.getResultObject();
            }

            logger.error("调用cif查询用户信息接口失败, result={}", result);
            return null;
        });
    }

    public UserInfo queryByUserCell(String cell) {
        if (StrUtil.isBlank(cell)) {
            return null;
        }

        serviceClient.handle(() -> {
            QueryResult<UserInfo> result = userQueryService.queryByUserCell(cell);
            if (result.isSuccess()) {
                return result.getResultObject() == null ? mockUserInfo(cell) :
                        result.getResultObject();
            }

            logger.error("调用cif查询用户信息接口失败, result={}", result);
            return null;
        });

        return null;
    }

    /**
     * 模拟用户数据
     */
    private UserInfo mockUserInfo(String userId) {
        UserInfo userInfo = new UserInfo();

        userInfo.setUserId(userId);
        userInfo.setRealName("张三四");
        userInfo.setCell("15957141550");

        return userInfo;
    }

}
